import { defineStore } from "pinia";
import { ref } from "vue";
import { platformConfigsStorage } from "@/config/storage";

/** 系统配置信息 */
export const useConfigStore = defineStore("config", () => {
    const config = ref<PlatformConfigs>({});

    /** 持久化设置全局配置 */
    const SET_PLATFORM_CONFIG = (_config: PlatformConfigs): void => {
        // 更新pinia状态
        Object.keys(_config).forEach((key) => {
            const _key = key as keyof PlatformConfigs;
            config.value[_key] = _config[_key] as any;
        });
        // 同步设置localStorage
        platformConfigsStorage.value = Object.assign(platformConfigsStorage.value ?? {}, _config);
    };

    /** 通过key获取配置项 */
    const GET_PLATFORM_CONFIG = <K extends keyof PlatformConfigs>(key: K): PlatformConfigs[K] => {
        return config.value[key];
    };

    return { config, SET_PLATFORM_CONFIG, GET_PLATFORM_CONFIG };
});
